package com.saicmotor.keeplive;

import android.app.Notification;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;

import androidx.annotation.Nullable;

import com.fanjun.keeplive.KeepLive;
import com.fanjun.keeplive.config.NotificationUtils;

/**
 * @Author xinxinzhang
 * @Date 2022/08/19
 **/
public class RemoteService extends Service {

    private MyBinder binder;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (binder == null) {
            binder = new MyBinder(this);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            bindService(new Intent(this, LocalService.class),
                    connection, Context.BIND_ABOVE_CLIENT);
        } catch (Exception ex) {
        }
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }

    private class MyBinder extends IMyAidlInterface.Stub {

        RemoteService service;

        MyBinder(RemoteService service) {
            this.service = service;
        }

        @Override
        public void wakeUp(String title, String discription, int iconRes) throws RemoteException {
            if (Build.VERSION.SDK_INT < 25) {
                Intent intent = new Intent(getApplicationContext(), NotificationClickReceiver.class);
                intent.setAction(NotificationClickReceiver.CLICK_NOTIFICATION);
                Notification notification =
                        NotificationUtils.createNotification(service, title, discription, iconRes, intent);
                service.startForeground(13691, notification);
            }
        }
    }

    private ServiceConnection connection = new MyServiceConnection(this);

    private class MyServiceConnection implements ServiceConnection {

        RemoteService service;

        MyServiceConnection(RemoteService service) {
            this.service = service;
        }

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Intent remote = new Intent(service, LocalService.class);
            service.startService(remote);
            Intent intent = new Intent(service, LocalService.class);
            service.bindService(intent, this, Context.BIND_ABOVE_CLIENT);
        }
    }

    private class NotificationClickReceiver extends BroadcastReceiver {
        static final String CLICK_NOTIFICATION = "CLICK_NOTIFICATION ";

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(CLICK_NOTIFICATION)) {
                if (KeepLive.foregroundNotification != null) {
                    if (KeepLive.foregroundNotification.getForegroundNotificationClickListener() != null) {
                        KeepLive.foregroundNotification.getForegroundNotificationClickListener().
                                foregroundNotificationClick(context, intent);
                    }
                }
            }
        }
    }
}
